package com.sharp.oss.service.impl;

import com.sharp.kernel.constant.db.SqlConstant;
import com.sharp.kernel.utils.ReflectionUtil;
import com.sharp.oss.dao.entity.SaBanner;
import com.sharp.oss.dao.repository.SaBannerService;
import com.sharp.oss.service.BannerService;
import com.sharp.oss.vo.banner.BannerRes;
import com.sharp.oss.vo.banner.DeleteBannerReq;
import com.sharp.oss.vo.banner.UpdateBannerReq;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Title: BannerServiceImpl
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2020/7/20 22:50
 * @description Project Name: Seed
 * @Package: com.srct.service.oss.service.impl
 */
@Service
public class BannerServiceImpl implements BannerService {

    @Resource
    private SaBannerService bannerService;

    @Override
    public List<BannerRes> getBanner(Integer bannerType) {
        List<SaBanner> bannerList = bannerService.getAllBanner(bannerType);
        return ReflectionUtil.copyList(bannerList, BannerRes.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBanner(UpdateBannerReq req) {
        SaBanner saBanner = ReflectionUtil.copy(req, SaBanner.class);
        if (saBanner.getId() == null) {
            if (saBanner.getDisplayOrder() == null) {
                int count = bannerService.countAllBanner(saBanner.getType());
                saBanner.setDisplayOrder(count + 1);
            }
            saBanner.setValid(SqlConstant.VALID);
            bannerService.insertSelective(saBanner);
        } else {
            bannerService.updateByPrimaryKeySelective(saBanner);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBanner(DeleteBannerReq req) {
        bannerService.disableByPrimaryKey(req.getId());
    }
}
